﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using Job.Web.Framework;

namespace Job.Web
{
    public class WebCommon
    {
        public static string GetApplicationPath(HttpRequest request)
        {
            string path = string.Empty;
            try
            {
                //if (request.ApplicationPath != "/")
                path = request.ApplicationPath;
            }
            catch (Exception e)
            {
                throw e;
            }

            return path;
        }
        // categoryHash中存储的是一级栏目ID，
        //  以及一级栏目下所有的二级栏目，二级栏目以List方式存放的BaseInfo元素
        // 如: 猎头专区下有猎头说明、联系方式等，那么存储的数据为
        //   categoryHash中 Key = 猎头专区的Id
        //   categoryHash中 值为 List，List中按顺序有 "猎头说明"、"联系方式"的BaseInfo结构
        private static Hashtable CategoryHash;
        private static Hashtable SubjectHash;
        private static Object LockObj = new Object();

        //根据某个一级栏目，得到一级栏目下所有二级栏目的配置信息
        public static List<BaseInfo> GetCategorySubList(string category)
        {
            lock (LockObj)
            {
                if (CategoryHash == null) CategoryHash = InitCategoryHash();
            }

            if (null == CategoryHash[category])
            {
                return new List<BaseInfo>();
            }
            else
            {
                return (List<BaseInfo>)CategoryHash[category];
            }
        }


        //根据某个二级栏目的ID，得到二级栏目的配置信息
        public static BaseInfo GetSubjectInfo(int subject)
        {
            return GetSubjectInfo(Convert.ToString(subject));
        }
        public static BaseInfo GetSubjectInfo(string subject)
        {
            lock (LockObj)
            {
                if (SubjectHash == null) SubjectHash = InitSubjectHash();
            }

            if (null == SubjectHash[subject])
            {
                return new BaseInfo();
            }
            else
            {
                return (BaseInfo)SubjectHash[subject];
            }
        }

        //初始化SubjectHash中的数据
        private static Hashtable InitSubjectHash()
        {
            Hashtable categorys = InitCategoryHash();
            Hashtable subjects = new Hashtable();
            foreach (List<BaseInfo> list in categorys.Values)
            {
                foreach (BaseInfo item in list)
                    subjects.Add(Convert.ToString(item.SubjectId), item);
            }
            return subjects;
        }




        //初始化CategoryHash中的数据
        private static Hashtable InitCategoryHash()
        {
            Hashtable hash = new Hashtable();
            hash.Add(Convert.ToString((int)Constants.CategoryType.Company), GetCompanyCategory());
            hash.Add(Convert.ToString((int)Constants.CategoryType.Hunter), GetHunterCategory());
            hash.Add(Convert.ToString((int)Constants.CategoryType.Service), GetServiceCategory());
            return hash;
        }
        private static List<BaseInfo> GetCompanyCategory()
        {
            List<BaseInfo> results = new List<BaseInfo>();

            BaseInfo item = new BaseInfo();
            item.TitleReadOnly = true;
            item.CatagoryId = (int)Constants.CategoryType.Company;
            item.SubjectId = (int)Constants.SubjectType.CompanyDescription;
            item.DisplayTitle = "关于无忧";
            item.ImageName = "item_about.png";
            item.IsList = false;
            results.Add(item);

            item = new BaseInfo();
            item.TitleReadOnly = true;
            item.CatagoryId = (int)Constants.CategoryType.Company;
            item.SubjectId = (int)Constants.SubjectType.CompanyNews;
            item.DisplayTitle = "无忧新闻";
            item.ImageName = "item_news.png";
            item.IsList = false;
            results.Add(item);

            item = new BaseInfo();
            item.TitleReadOnly = false;
            item.CatagoryId = (int)Constants.CategoryType.Company;
            item.SubjectId = (int)Constants.SubjectType.CompanyFastList;
            item.DisplayTitle = "无忧快讯";
            item.ImageName = "item_1.png";
            item.IsList = true;
            results.Add(item);

            item = new BaseInfo();
            item.TitleReadOnly = false;
            item.CatagoryId = (int)Constants.CategoryType.Company;
            item.SubjectId = (int)Constants.SubjectType.CompanyHRList;
            item.DisplayTitle = "HR频道";
            item.ImageName = "item_2.png";
            item.IsList = true;
            results.Add(item);

            item = new BaseInfo();
            item.TitleReadOnly = false;
            item.CatagoryId = (int)Constants.CategoryType.Company;
            item.SubjectId = (int)Constants.SubjectType.CompanyManagerList;
            item.DisplayTitle = "管理资讯";
            item.ImageName = "item_3.png";
            item.IsList = true;
            results.Add(item);

            item = new BaseInfo();
            item.TitleReadOnly = false;
            item.CatagoryId = (int)Constants.CategoryType.Company;
            item.SubjectId = (int)Constants.SubjectType.CompanyIndustryList;
            item.DisplayTitle = "行业动态";
            item.ImageName = "item_4.png";
            item.IsList = true;
            results.Add(item);

            item = new BaseInfo();
            item.TitleReadOnly = true;
            item.CatagoryId = (int)Constants.CategoryType.Company;
            item.SubjectId = (int)Constants.SubjectType.CompanyBBS;
            item.DisplayTitle = "无忧论坛";
            item.ImageName = "item_5.png";
            item.IsList = false;
            results.Add(item);

            item = new BaseInfo();
            item.TitleReadOnly = true;
            item.CatagoryId = (int)Constants.CategoryType.Company;
            item.SubjectId = (int)Constants.SubjectType.CompanyContactMethod;
            item.DisplayTitle = "联系我们";
            item.ImageName = "item_contact.png";
            item.IsList = false;
            results.Add(item);

            return results;
        }

        private static List<BaseInfo> GetHunterCategory()
        {
            List<BaseInfo> results = new List<BaseInfo>();

            BaseInfo item = new BaseInfo();
            item.TitleReadOnly = true;
            item.CatagoryId = (int)Constants.CategoryType.Hunter;
            item.SubjectId = (int)Constants.SubjectType.HunterDescription;
            item.DisplayTitle = "猎头说明";
            item.ImageName = "item_about.png";
            item.IsList = false;
            results.Add(item);

            item = new BaseInfo();
            item.TitleReadOnly = false;
            item.CatagoryId = (int)Constants.CategoryType.Hunter;
            item.SubjectId = (int)Constants.SubjectType.HunterPositionList;
            item.DisplayTitle = "猎头职位";
            item.ImageName = "item_1.png";
            item.LinkUrl = "";
            item.IsList = true;
            results.Add(item);

            item = new BaseInfo();
            item.TitleReadOnly = false;
            item.CatagoryId = (int)Constants.CategoryType.Hunter;
            item.SubjectId = (int)Constants.SubjectType.HunterNewsList;
            item.DisplayTitle = "猎头资讯";
            item.ImageName = "item_2.png";
            item.IsList = true;
            results.Add(item);

            item = new BaseInfo();
            item.TitleReadOnly = true;
            item.CatagoryId = (int)Constants.CategoryType.Hunter;
            item.SubjectId = (int)Constants.SubjectType.HunterManagerArea;
            item.DisplayTitle = "管理专区";
            item.ImageName = "item_file.png";
            item.IsList = false;
            results.Add(item);

            item = new BaseInfo();
            item.TitleReadOnly = true;
            item.CatagoryId = (int)Constants.CategoryType.Hunter;
            item.SubjectId = (int)Constants.SubjectType.HunterContactMethod;
            item.DisplayTitle = "联系方式";
            item.ImageName = "item_contact.png";
            item.IsList = false;
            results.Add(item);

            return results;
        }

        private static List<BaseInfo> GetServiceCategory()
        {
            List<BaseInfo> results = new List<BaseInfo>();

            BaseInfo item = new BaseInfo();
            item.TitleReadOnly = true;
            item.CatagoryId = (int)Constants.CategoryType.Service;
            item.SubjectId = (int)Constants.SubjectType.ServiceDescription;
            item.DisplayTitle = "业务说明";
            item.ImageName = "item_about.png";
            item.IsList = false;
            results.Add(item);

            item = new BaseInfo();
            item.TitleReadOnly = true;
            item.CatagoryId = (int)Constants.CategoryType.Service;
            item.SubjectId = (int)Constants.SubjectType.ServiceSuggestion;
            item.DisplayTitle = "解决方案";
            item.ImageName = "item_file.png";
            item.IsList = false;
            results.Add(item);

            item = new BaseInfo();
            item.TitleReadOnly = false;
            item.CatagoryId = (int)Constants.CategoryType.Service;
            item.SubjectId = (int)Constants.SubjectType.ServiceQuestionList;
            item.DisplayTitle = "政策咨询";
            item.ImageName = "item_1.png";
            item.IsList = true;
            results.Add(item);

            item = new BaseInfo();
            item.TitleReadOnly = false;
            item.CatagoryId = (int)Constants.CategoryType.Service;
            item.SubjectId = (int)Constants.SubjectType.ServiceAnalysisList;
            item.DisplayTitle = "案例分析";
            item.ImageName = "item_2.png";
            item.IsList = true;
            results.Add(item);

            item = new BaseInfo();
            item.TitleReadOnly = true;
            item.CatagoryId = (int)Constants.CategoryType.Service;
            item.SubjectId = (int)Constants.SubjectType.ServiceContactMethod;
            item.DisplayTitle = "联系方式";
            item.ImageName = "item_contact.png";
            item.IsList = false;
            results.Add(item);

            return results;
        }

    }

    public struct BaseInfo
    {
        //需要用get,set方式来设置成属性
        //如果不用set,get则表示为Field则在梆定的时候不能访问。

        public bool TitleReadOnly {get;set;}  //标题是否为固定
        public int MaxDescSize { get; set; }      //简介字段的长度，简介字段主要用在首页
        public int SubjectId { get; set; }        //栏目所对应的内部编号
        public string DisplayTitle { get; set; }  //显示在二级页面左边的标题
        public string ImageName { get; set; }    //显示在二级页面左边的图标
        public int CatagoryId { get; set; }    //二级栏目所对应的一级栏目ID
        public bool IsList { get; set; }          //二级栏目所对应的是否是列表页面
        public string LinkUrl { get; set; }       //如果二级栏目需要特殊处理（如猎头职位），直接设置需要链接的地址
    }
}
